<script setup lang="ts">
  import { ref } from 'vue'
  import type { GetCodeParams } from '@/types/user'
  import type { InputOnBlurEvent } from '@uni-helper/uni-app-types/index'
  import { getCodeApi, loginByMobileApi } from '@/api/user'
  import { useCounterStore } from '@/stores/counter'
  const store = useCounterStore()
  defineOptions({
    options: {
      styleIsolation: 'shared',
    },
  })
  // 1. 倒计时处理
  const showCountDown = ref(false)
  const isAgree = ref(false)
  const form = ref<UniHelper.UniFormsInstance>()
  const aaa = ref<UniHelper.UniFormsItemInstance>()
  const bbb = ref<UniHelper.UniFormsItemInstance>()
  const getMobileCode = async () => {
    const res = await aaa.value?.onFieldChange?.(formdata.value.mobile)
    if (res === null) {
      showCountDown.value = true
      // 发送请求 获取手机验证码
      const params: GetCodeParams = {
        mobile: formdata.value.mobile,
        type: 'login',
      }
      const resCode = await getCodeApi(params)
      console.log(resCode)
    } else {
      uni.showToast({ title: '手机号码不合法', icon: 'none' })
    }
  }
  // 定义表单数据对象
  const formdata = ref({
    mobile: '13230000003',
    code: '',
  })
  // 定义表单验证规则！！
  const formRules = {
    mobile: {
      rules: [
        { required: true, errorMessage: '请填写手机号码' },
        { pattern: '^[1][3-8][0-9]{9}$', errorMessage: '手机号码格式不正确' },
      ],
    },
    code: {
      rules: [
        { required: true, errorMessage: '请输入验证码' },
        { pattern: '^[0-9]{6}$', errorMessage: '验证码格式不正确' },
      ],
    },
  }
  // 登录功能
  const onSubmit = async () => {
    // 用form组件的统一的校验功能 (自动校验多个字段)
    try {
      await form.value?.validate?.()
      if (!isAgree.value) {
        // 登录成功
        uni.showToast({ title: '您还没有勾选用户协议', icon: 'none' })
        return
      }

      // 验证都通过了
      console.log('验证都通过了')
      const res = await loginByMobileApi(formdata.value)
      console.log(res)
      store.setToken(res.token)

      // 跳转回 上一页
      uni.navigateBack()
    } catch (error) {
      console.log('不通过')
    }
  }
</script>

<template>
  <uni-forms class="login-form" ref="form" :model="formdata" :rules="formRules">
    <uni-forms-item name="mobile" ref="aaa">
      <uni-easyinput
        :input-border="false"
        :clearable="false"
        placeholder="请输入手机号"
        placeholder-style="color: #C3C3C5"
        v-model="formdata.mobile"
        @blur="(e:InputOnBlurEvent) => aaa?.onFieldChange?.(e.detail.value)"
      />
    </uni-forms-item>
    <uni-forms-item name="code" ref="bbb">
      <uni-easyinput
        :input-border="false"
        :clearable="false"
        v-model="formdata.code"
        placeholder="请输入验证码"
        placeholder-style="color: #C3C3C5"
        @blur="(e:InputOnBlurEvent) => bbb?.onFieldChange?.(e.detail.value)"
      />
      <text class="text-button" v-if="!showCountDown" @click="getMobileCode">获取验证码</text>
      <view class="text-button" v-else>
        <uni-countdown
          @timeup="showCountDown = false"
          :show-day="false"
          :show-hour="false"
          :show-minute="false"
          :second="59"
        />秒后重新发送
      </view>
    </uni-forms-item>

    <view class="agreement">
      <radio :checked="isAgree" @click="isAgree = !isAgree" color="#16C2A3" />
      我已同意
      <text class="link">用户协议</text>
      及
      <text class="link">隐私协议</text>
    </view>

    <button class="uni-button" @click="onSubmit">登 录</button>
  </uni-forms>
</template>
<style lang="scss">
  @import 'styles.scss';
</style>
